<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
/*==========================*/
namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;
use DB;
use Symfony\Component\HttpFoundation\Request;

use Auth;

class HomeController extends Controller
{
    //地址录入
    public function fpage()
    {   
        return view('home.fpage');
    }
    //地址录入处理
    public function doCity(){
   
    	$name=$_POST['name'];
        if($name!=""){
        	$sql= DB::table('city')->where('name','like','%'.$name.'%')->get();
        	// dd($sql);

        	if($sql){
        		return(json_encode($sql));	
        	}
        	else{
        		return "没有该市";
        	}
        }
    }


    //商家展示页面
    public function shopp($mid){
      //食品信息查询
        $sql= DB::table('foodbar')->get();


        $mid=$mid;
        $sql2= DB::table('goods')->where('mid','=',$mid)->get();
      //商家信息查询

       
        $sql3= DB::table('merchant')->where('id','=',$mid)->first();
      //评论模块
        
        //用户评论
        $sql5= DB::table('yonghupinglun')->where('mid','=',$mid)->where('dafen','<','3')->get();
        $sql6= DB::table('yonghupinglun')->where('mid','=',$mid)->where('dafen','>','2')->get();
        //商家回复
        $sql7= DB::table('shangjiahuifu')->where('mid','=',$mid)->get();

        //好评条数
        //$sql8='select count() from yonghupinglun where(dafen<"3")';
        $sql8= DB::table('yonghupinglun')->where('mid','=',$mid)->where('dafen','<','3')->count();
        $sql9= DB::table('yonghupinglun')->where('mid','=',$mid)->where('dafen','>','2')->count();
        //mid
        $sql10=2;





        //echo "$sql8";
        // $users = DB::table('yonghupinglun')
        //     ->leftJoin('user_info','yonghupinglun.uid', '=' ,'user_info.uid')
        //     //->select("yonghupinglun.time","yonghupinglun.comment","yonghupinglun.dafen","yonghupinglun.goods","user_info.photo","yonghupinglun.mid")
        //     ->select("yonghupinglun.mid","=",$mid)
        //     ->get();
        


        
        return view('home.shopp')->with('sql',$sql)->with('sql2',$sql2)->with('sql3',$sql3)->with('sql5',$sql5)->with('sql6',$sql6)->with('sql7',$sql7)->with('sql8',$sql8)->with('sql9',$sql9)->with('sql10',$sql10);

    }
    //ajax无刷新搜索处理
    public function doBreak(){
        $data=$_GET['data'];
        //dd($data);
        //comsole.log($data);
        //alert($data);die;
        if($data!=""){
            $sql4= DB::table('goods')->where('name','like','%'.$data.'%')->get();
            //dd($sql);
            if($sql4){
                //return view('home.tihuan')->with('sql4',$sql4);
                return(json_encode($sql4));  
            }
            else{
                return "没有该食品";
           }
        }

        //商户ID
        $id=1;
        $sql2= DB::table('goods')->where('mid','=',$id)->get();
      
        return view('home.shop',['sql'=>$sql],['sql2'=>$sql2]);

    }

    //收藏处理
    public function shoucang(){

        $uid=$_GET['uid'];
        $mid=$_GET['mid'];
       // echo"$mid";die;
      
        //var_dump($mid);
        //comsole.log($mid);
        $s=DB::table('shoucang')->where('mid','=',$mid)->where('uid','=',$uid)->get();
        if($s){
            return("您已收藏过该商品");
        }
        else{
            $sql=DB::table('shoucang')->insert(['uid' => $uid,'mid' => $mid]);
            if($sql){
                return("收藏成功");  
            }
        }
    }

    //取消收藏处理
    public function bshoucang(){
        $mid=$_GET['mid'];
        $uid=$_GET['uid'];
        //var_dump($mid);
        //comsole.log($mid);
        $s=DB::table('shoucang')->where('mid','=',$mid)->where('uid','=',$uid)->get();
        if($s){
            DB::table('shoucang')->where('mid','=',$mid)->where('uid','=',$uid)->delete();
            return("您已取消收藏");
        }

    } 

    //删除收藏处理
    public function delsc($mid){
        //$data = $request->except('_token');
        $uid=100;//session
        $s=DB::table('shoucang')->where('mid','=',$mid)->where('uid','=',$uid)->get();
        if($s){
            $a=DB::table('shoucang')->where('mid','=',$mid)->where('uid','=',$uid)->delete();
            // if($a){
            //     $sql = DB::table('shoucang')
            //     ->leftJoin('merchant','shoucang.mid', '=' ,'merchant.id')
            //     ->select('merchant.name','merchant.delivery','merchant.send','merchant.photo','merchant.transit','merchant.sales','shoucang.mid','shoucang.uid')
            //     ->where('shoucang.uid', '=', $uid)
            //     ->get();
        



            //     //->with('sql',$sql)
            //     return view('home.shoucang')->with('sql',$sql);
                
            // }
            
            //alert("您已取消收藏");
            return redirect("showsc");
        }

    } 
    


    //收藏展示页面    
    public function showsc(){        
        //$uid 在session里取
        $uid=100;
        $sql = DB::table('shoucang')
            ->leftJoin('merchant','shoucang.mid', '=' ,'merchant.id')
            ->select('merchant.name','merchant.delivery','merchant.send','merchant.photo','merchant.transit','merchant.sales','shoucang.mid','shoucang.uid')
            ->where('shoucang.uid', '=', $uid)
            ->get();
        
        $sql2=DB::table('shoucang')->where('uid','=',$uid)->count();


        //->with('sql',$sql)
        return view('home.shoucang')->with('sql',$sql)->with('sql2',$sql2);
    }   

}
